﻿Shader "RenderingPractice/Phong Vertex-Level"//逐顶点光照（Phong）
{
    Properties
    {
        _Diffuse ("Diffuse", Color) = (1,1,1,1)//漫反射颜色
		_Specular("Specular",Color) = (1,1,1,1)//镜面反射颜色
		_Gloss("Gloss(光泽度)",Range(8.0,256.0)) = 20//光泽度（表面光滑度，影响高光大小）
    }
    SubShader
    {
        Tags { "LightMode"="ForwardBase" }

        Pass
        {
            CGPROGRAM
            #pragma vertex VertexFunction
            #pragma fragment FragmentFunction

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
			
            fixed4 _Diffuse;//漫反射颜色
			fixed4 _Specular;//镜面反射颜色
			float _Gloss;//光泽度（表面光滑度，影响高光大小）

            struct VertexIn
            {
                float4 vertex : POSITION; 
				float3 normal :NORMAL;//模型空间表面法线
            };

            struct VertexOut
            {
                float4 vertex : SV_POSITION;
				fixed3 vertexColor:COLOR;
            };
			//顶点函数
            VertexOut VertexFunction (VertexIn vi)
            {
                VertexOut vo;
				//1.环境光：
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
				//2.漫反射：
				fixed3 worldNormalDir = UnityObjectToWorldNormal(vi.normal);//n
				fixed4 worldPos = mul(unity_ObjectToWorld ,vi.vertex );
				//fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);//l
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(worldPos.xyz));//l
				fixed3 diffuseColor = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormalDir ,worldLightDir));
				//3.镜面反射颜色（Phong）
				fixed3 reflectDIr = normalize(reflect(-_WorldSpaceLightPos0.xyz ,worldNormalDir));//r 
				//fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld ,vi.vertex ).xyz);
				fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos.xyz));
				fixed3 specularColor = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(viewDir ,reflectDIr)) ,_Gloss);
				//3.光照颜色：
				vo.vertexColor = ambient + diffuseColor + specularColor;
				//顶点坐标：模型空间=>裁剪空间
                vo.vertex = UnityObjectToClipPos(vi.vertex);
                return vo;
            }
			//片元函数
            fixed4 FragmentFunction (VertexOut fi) : SV_Target
            {
				fixed4 c = fixed4(fi.vertexColor ,1.0);
                return c;
            }
            ENDCG
        }
    }
	FallBack "Specular"
}
